package com.xju.subjectsystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xju.subjectsystem.mapper.CourseMapper;
import com.xju.subjectsystem.pojo.Course;
import com.xju.subjectsystem.pojo.PageResult;
import com.xju.subjectsystem.service.CourseService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * -*- coding:utf-8 -*-
 *
 * @projectName: Subjectsystem
 * @package: com.xju.subjectsystem.service.impl
 * @className: CourseServiceImpl
 * @author: 15171
 * @description: TODO : 
 * @date: 2024-12-08 22:45
 */
@Service
public class CourseServiceImpl implements CourseService {
    private final CourseMapper courseMapper;

    public CourseServiceImpl(CourseMapper courseMapper) {
        this.courseMapper = courseMapper;
    }

    @Override
    public List<Course> showAllCourse() {
        return courseMapper.showAllCourse();
    }

    @Override
    public PageResult selectByDepart(Integer page, Integer pageSize, String depart) {
        PageHelper.startPage(page, pageSize);
        List<Course> courseList = courseMapper.selectByDepart(depart);
        Page<Course> p = (Page<Course>) courseList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public PageResult selectByName(Integer page, Integer pageSize, String cname) {
        PageHelper.startPage(page, pageSize);
        List<Course> courseList = courseMapper.selectByName(cname);
        Page<Course> p = (Page<Course>) courseList;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public void add(Course course) {
        //添加一门课程
        courseMapper.insert(course);
    }

    @Override
    public Map<String, List<String>> batchAddCourses(List<Course> courseList) {
        List<String> successList = new ArrayList<>();
        List<String> failList = new ArrayList<>();

        for (Course course : courseList) {
            try {
                this.add(course);
                successList.add("课程：" + course.getCname()+ " 导入成功");
            } catch (Exception e) {
                failList.add("课程：" + course.getCname() + " 导入失败，原因：数据异常");
            }
        }

        // 将结果封装成Map返回
        Map<String, List<String>> resultData = new HashMap<>();
        resultData.put("successList", successList);
        resultData.put("failList", failList);

        return resultData;

    }

    @Override
    public void delete(Course course) {
        //删除一门课程
        courseMapper.deleteByPrimaryKey(course.getCno());
    }

    @Override
    public void update(Course course) {
        //修改一门课程的信息
        courseMapper.updateByPrimaryKeySelective(course);
    }


    public boolean cnoIsExist(String cno) {
        return false;
    }
}
